﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;

namespace it_security_project.App_Code.BusinessLogicLayer
{
    public class Thread : BusinessObjectBase
    {
        private string _Author;
        private string _Title;
        private string _Topic;
        private List<Comment> _Comments;

        public int ThreadID
        {
            get;
            set;
        }

        public List<Comment> Comments
        {
            get
            {
                return this._Comments;
            }
            set
            {
                this._Comments = value;
            }

        }

        public string Author
        {
            get
            {
                return this._Author;
            }
            set
            {
                this.ValidationErrors.Remove("ThreadAuthor");

                if (String.IsNullOrWhiteSpace(value))
                {
                    this.ValidationErrors.Add("ThreadAuthor", "You have to write an author");
                }
                else if (value.Length > 30)
                {
                    this.ValidationErrors.Add("ThreadAuthor", "The author can't be more then 30 characters");
                }
                this._Author = value != null ? value.Trim() : null;
            }
        }

        public string Title
        {
            get
            {
                return this._Title;
            }
            set
            {
                this.ValidationErrors.Remove("ThreadTitle");

                if(String.IsNullOrWhiteSpace(value))
                {
                    this.ValidationErrors.Add("ThreadTitle", "You have to write a Thread Title");
                }
                else if(value.Length > 50)
                {
                    this.ValidationErrors.Add("ThreadTitle", "The Title can't be more then 50 characters");
                }
                else if (value != Regex.Replace(value, "<.*?>", string.Empty))
                {
                    this.ValidationErrors.Add("ThreadTitle", "The Title contains characters that are not allowed");
                }
                this._Title = value != null ? value.Trim() : null;
            }
        }

        public string Topic
        {
            get
            {
                return this._Topic;
            }
            set
            {
                this.ValidationErrors.Remove("ThreadTopic");

                if (String.IsNullOrWhiteSpace(value))
                {
                    this.ValidationErrors.Add("ThreadTopic", "You have to write a Topic!");
                }
                else if (value.Length > 500)
                {
                    this.ValidationErrors.Add("ThreadTopic", "The topic can't be over 500 characters!");
                }
                else if (value != Regex.Replace(value, "<.*?>", string.Empty))
                {
                    this.ValidationErrors.Add("ThreadTopic", "The topic contains characters that are not allowed");
                }
                this._Topic = value != null ? value.Trim() : null;
            }
        }

        public Thread()
        {
        }

        public Thread(int threadID, string author, string title, string topic)
        {
            this.ThreadID = threadID;
            this.Author = author;
            this.Title = title;
            this.Topic = topic;
        }

        public Thread(int threadID, string author, string title, string topic, List<Comment> comments)
        {
            this.ThreadID = threadID;
            this.Author = author;
            this.Title = title;
            this.Topic = topic;
            
        }
    }
}